package com.kernel.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

/****************************************
 * @name EchoThread
 * @description
 * @author 黄彬恒
 * @since 2018/8/24
 ***************************************/
public class EchoThread implements Runnable {
    private Socket client;

    EchoThread(Socket socket) {
        this.client = socket;
    }

    @Override
    public void run() {
        BufferedReader buf;
        PrintStream out;
        try {
            out = new PrintStream(client.getOutputStream());
            buf = new BufferedReader(new InputStreamReader(client.getInputStream()));// 接收客户端输入信息
            boolean f = true;
            while (f) {
                String str = buf.readLine();
                if (str == null || "".equals(str)) {
                    f = false;
                } else {
                    if ("bye".equals(str)) {
                        f = false;
                    } else {
                        out.println("echo:" + str);
                    }
                }
            }
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
